package com.util;

import com.pojo.SystemMenu;

import java.util.ArrayList;
import java.util.List;

public class TreeUtil {
    // 模拟数据  0
    public static List<SystemMenu> toTree(List<SystemMenu> treeList, Integer pid) {
        List<SystemMenu> retList = new ArrayList<SystemMenu>();
//        循环遍历
        for (SystemMenu parent : treeList) {
//            查询每一个pid
            if (pid.equals(parent.getPid())) {
                // 查询到 顶级 pid
                retList.add(findChildren(parent, treeList));
            }
        }
        return retList;
    }
//    模拟数据  顶级 菜单实体类
    private static SystemMenu findChildren(SystemMenu parent, List<SystemMenu> treeList) {
//        循环遍历所有 菜单
        for (SystemMenu child : treeList) {
//            如果传入的实体类id  等于
            if (parent.getId().equals(child.getPid())) {
                if (parent.getChild() == null) {
                    parent.setChild(new ArrayList<SystemMenu>());
                }
                parent.getChild().add(findChildren(child, treeList));
            }
        }
        return parent;
    }
}